﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

using H2.NET.Business;
using H2.NET.Controls;

namespace H2.NET.Web.Pages
{
    public partial class BPM_CN116_UserChoose : TaskControl
    {
        #region 属性 Public
        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        private bool IsUsed
        {
            get
            {
                return ViewState["IsUsed"] == null ? false : (bool)ViewState["IsUsed"];
            }
            set { ViewState["IsUsed"] = value; }
        }

        public bool isReturned
        {
            get
            {
                return ViewState["isReturned"] == null ? false : (bool)ViewState["isReturned"];
            }
            set { ViewState["isReturned"] = value; }
        }

        public string MDCActions
        {
            get
            {
                return ViewState["MDCActions"] == null ? "" : (string)ViewState["MDCActions"]; ;
            }
            set { ViewState["MDCActions"] = value; }
        }

        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

            //如果当前部门不需要显示，退出
            if (IsLoaded && !IsUsed)
            {
                this.Visible = false;
                return;
            }
            if (!Page.IsPostBack)
            {
                if (TaskAPI.Process == "Authority To Recruit")
                    isReturned = (Parent.Page as PATR).Assessment1.GetReturnStatus();
                else if (TaskAPI.Process == "Headcount Budget")
                    isReturned = (Parent.Page as PBGD).Assessment1.GetReturnStatus();
                //zph 添加hr流程 的approver步骤 final approve步骤
                SetVisible(false, USR_Approver_L, USR_Approver, USR_Approver_C);
                SetVisible(false, USR_Final_Approver_L, USR_Final_Approver, USR_Final_Approver_C);
                SetVisible(false, USR_3rd_Approver_L, USR_3rd_Approver, USR_3rd_Approver_C);
                SetVisible(false, USR_4th_Approver_L, USR_4th_Approver, USR_4th_Approver_C);
                SetVisible(false, USR_5th_Approver_L, USR_5th_Approver, USR_5th_Approver_C);
                SetVisible(false, USR_6th_Approver_L, USR_6th_Approver, USR_6th_Approver_C);
                SetVisible(false, USR_7th_Approver_L, USR_7th_Approver, USR_7th_Approver_C);
                SetVisible(false, USR_Inform_Recruiter_L, USR_Inform_Recruiter, USR_Inform_Recruiter_C);

                IsUsed = false;
                if (!isReturned)
                {
                    IList<t_UserChoose> tUCs = (t_UserChoose.process == TaskAPI.Process & t_UserChoose.step == TaskAPI.Step).List<t_UserChoose>();
                    foreach (t_UserChoose tUC in tUCs)
                    {
                        //zph 添加hr流程 的approver步骤 final approve步骤
                        if (((object[])TaskAPI["Approver"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Approver").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_Approver_L, USR_Approver_C, USR_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Final_Approver"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Final_Approver").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_Final_Approver_L, USR_Final_Approver_C, USR_Final_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Third_Manager"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Third_Manager").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_3rd_Approver_L, USR_3rd_Approver_C, USR_3rd_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Fouth_Manager"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Fouth_Manager").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_4th_Approver_L, USR_4th_Approver_C, USR_4th_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Fifth_Manager"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Fifth_Manager").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_5th_Approver_L, USR_5th_Approver_C, USR_5th_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Sixth_Manager"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Sixth_Manager").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_6th_Approver_L, USR_6th_Approver_C, USR_6th_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Seventh_Manager"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Seventh_Manager").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_7th_Approver_L, USR_7th_Approver_C, USR_7th_Approver); IsUsed = true; }
                        if (((object[])TaskAPI["Inform_Recruiter"])[0].ToString() != "1" && tUC.UserChoose.ToLower() == ("Inform_Recruiter").ToLower() && (tUC.Actions.ToLower().Trim() == MDCActions.ToLower() || tUC.Actions.Trim() == "*")) { SetVisible(true, USR_Inform_Recruiter_L, USR_Inform_Recruiter_C, USR_Inform_Recruiter); IsUsed = true; }


                    }
                }
                this.Visible = IsUsed;
                this.IsLoaded = true;

                SetEnable(TaskAPI.IsAllowSubmit, USR_Approver, USR_Final_Approver_C, USR_Final_Approver, USR_Inform_Recruiter, USR_3rd_Approver_C, USR_3rd_Approver, USR_4th_Approver_C, USR_4th_Approver, USR_5th_Approver_C, USR_5th_Approver, USR_6th_Approver_C, USR_6th_Approver, USR_7th_Approver_C, USR_7th_Approver);

                SetForeColor(Color.Blue, USR_Approver_L, USR_Final_Approver_L, USR_Inform_Recruiter_L, USR_3rd_Approver_L, USR_4th_Approver_L, USR_5th_Approver_L, USR_6th_Approver_L, USR_7th_Approver_L);
                USR_Approver_C.Checked = true;
                USR_Final_Approver_C.Checked = true;
                USR_3rd_Approver_C.Checked = true;
                USR_4th_Approver_C.Checked = true;
                USR_5th_Approver_C.Checked = true;
                USR_6th_Approver_C.Checked = true;
                USR_7th_Approver_C.Checked = true;
                USR_Inform_Recruiter_C.Checked = true;
            }
        }
        #endregion

        #region 事件 用户选择
        protected void USR_TextChanged(object sender, EventArgs e)
        {
            if (!isReturned)
            {
                try
                {
                    string SQL = "select rtrim(NAME) as username, rtrim(FULLNAME) AS fullname from jobs where rtrim(name)='" + ((Dialog)sender).Text + "'";
                    TaskDB.Log("", true, "UserChoose - USR_TextChanged:" + SQL);

                    DBAPI.Storage("bpmDB1");
                    DataTable dt = DBAPI.Query(SQL);

                    //根据查询结果进行赋值
                    if (dt.Rows.Count > 0)
                    {
                        ((Dialog)sender).Text = dt.Rows[0]["FULLNAME"].ToString();
                        switch (((Dialog)sender).ID)
                        {
                            //zph 添加hr流程 的approver步骤 final approve步骤
                            case "USR_Approver":
                                TaskAPI["Approver"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_Final_Approver":
                                TaskAPI["Final_Approver"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_3rd_Approver":
                                TaskAPI["Third_Manager"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_4th_Approver":
                                TaskAPI["Fouth_Manager"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_5th_Approver":
                                TaskAPI["Fifth_Manager"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_6th_Approver":
                                TaskAPI["Sixth_Manager"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_7th_Approver":
                                TaskAPI["Seventh_Manager"] = dt.Rows[0]["username"].ToString();
                                break;
                            case "USR_Inform_Recruiter":

                                string SQL_1 = " SELECT     TOP 1 RTRIM(EMAIL) AS EMAIL FROM  CLIENTS C WHERE      RTRIM(C.USERNAME) ='" + dt.Rows[0]["username"].ToString() + "'";

                                DBAPI.Storage("bpmDB1");
                                DataTable dt_1 = DBAPI.Query(SQL_1);
                                TaskDB.Log("", true, "UserChoose - USR_TextChanged:" + SQL_1);

                                //根据查询结果进行赋值
                                if (dt_1.Rows.Count > 0)
                                {
                                    TaskAPI["Inform_Recruiter"] = dt_1.Rows[0]["EMAIL"].ToString();
                                    TaskAPI["R_Inform_Recruiter"] = dt.Rows[0]["username"].ToString();

                                }

                                break;
                        }
                    }
                    else
                    {
                        ((Dialog)sender).Text = "";
                        switch (((Dialog)sender).ID)
                        {
                            //zph 添加hr流程 的approver步骤 final approve步骤
                            case "USR_Approver":
                                TaskAPI["Approver"] = "";
                                break;
                            case "USR_Final_Approver":
                                TaskAPI["Final_Approver"] = "";
                                break;
                            case "USR_3rd_Approver":
                                TaskAPI["Third_Manager"] = "";
                                break;
                            case "USR_4th_Approver":
                                TaskAPI["Fouth_Manager"] = "";
                                break;
                            case "USR_5th_Approver":
                                TaskAPI["Fifth_Manager"] = "";
                                break;
                            case "USR_6th_Approver":
                                TaskAPI["Sixth_Manager"] = "";
                                break;
                            case "USR_7th_Approver":
                                TaskAPI["Seventh_Manager"] = "";
                                break;
                            case "USR_Inform_Recruiter":
                                TaskAPI["Inform_Recruiter"] = "";
                                break;
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        #endregion

        #region 保存审批结果
        public void SaveStatus(string ApproveResult)
        {

        }
        #endregion

        #region 事件 USR_CheckBox 选择
        protected void USR_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (!isReturned)
            {
                switch (((Anthem.CheckBox)sender).ID)
                {
                    //zph 添加hr流程 的approver步骤 final approve步骤
                    case "USR_Approver_C":
                        SetForeColor(USR_Approver_C.Checked ? Color.Blue : Color.Black, USR_Approver_L);
                        TaskAPI["Approver"] = "";
                        break;
                    case "USR_Final_Approver_C":
                        SetForeColor(USR_Final_Approver_C.Checked ? Color.Blue : Color.Black, USR_Final_Approver_L);
                        TaskAPI["Final_Approver"] = "";
                        break;
                    case "USR_3rd_Approver_C":
                        SetForeColor(USR_3rd_Approver_C.Checked ? Color.Blue : Color.Black, USR_3rd_Approver_L);
                        TaskAPI["Third_Manager"] = "";
                        break;
                    case "USR_4th_Approver_C":
                        SetForeColor(USR_4th_Approver_C.Checked ? Color.Blue : Color.Black, USR_4th_Approver_L);
                        TaskAPI["Fouth_Manager"] = "";
                        break;
                    case "USR_5th_Approver_C":
                        SetForeColor(USR_5th_Approver_C.Checked ? Color.Blue : Color.Black, USR_5th_Approver_L);
                        TaskAPI["Fifth_Manager"] = "";
                        break;
                    case "USR_6th_Approver_C":
                        SetForeColor(USR_6th_Approver_C.Checked ? Color.Blue : Color.Black, USR_6th_Approver_L);
                        TaskAPI["Sixth_Manager"] = "";
                        break;
                    case "USR_7th_Approver_C":
                        SetForeColor(USR_7th_Approver_C.Checked ? Color.Blue : Color.Black, USR_7th_Approver_L);
                        TaskAPI["Seventh_Manager"] = "";
                        break;
                    case "USR_Inform_Recruiter_C":
                        SetForeColor(USR_Inform_Recruiter_C.Checked ? Color.Blue : Color.Black, USR_Inform_Recruiter_L);
                        TaskAPI["Inform_Recruiter"] = "";
                        break;
                }
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!IsUsed)
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            //zph 添加hr流程 的approver步骤 final approve步骤
            if (USR_7th_Approver_C.Checked && USR_7th_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_7th_Approver.Text.ToString().Trim(), "7th Manager  必填"));

                if (string.IsNullOrEmpty(USR_6th_Approver.Text) || string.IsNullOrEmpty(USR_5th_Approver.Text) || string.IsNullOrEmpty(USR_4th_Approver.Text) || string.IsNullOrEmpty(USR_3rd_Approver.Text) || string.IsNullOrEmpty(USR_Final_Approver.Text))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", "请选择 7th Approver之前的经理"));
                }
            }
            else if (USR_6th_Approver_C.Checked && USR_6th_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_6th_Approver.Text.ToString().Trim(), "6th Manager  必填"));

                if (string.IsNullOrEmpty(USR_5th_Approver.Text) || string.IsNullOrEmpty(USR_4th_Approver.Text) || string.IsNullOrEmpty(USR_3rd_Approver.Text) || string.IsNullOrEmpty(USR_Final_Approver.Text))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", "请选择 6th Approver之前的经理"));
                }
            }

            else if (USR_5th_Approver_C.Checked && USR_5th_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_5th_Approver.Text.ToString().Trim(), "5th Manager  必填"));

                if (string.IsNullOrEmpty(USR_4th_Approver.Text) || string.IsNullOrEmpty(USR_3rd_Approver.Text) || string.IsNullOrEmpty(USR_Final_Approver.Text))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", "请选择 5th Approver之前的经理"));
                }
            }
            else if (USR_4th_Approver_C.Checked && USR_4th_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_4th_Approver.Text.ToString().Trim(), "4th Manager  必填"));

                if (string.IsNullOrEmpty(USR_3rd_Approver.Text) || string.IsNullOrEmpty(USR_Final_Approver.Text))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", "请选择4th Approver之前的经理"));
                }
            }
            else if (USR_3rd_Approver_C.Checked && USR_3rd_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_3rd_Approver.Text.ToString().Trim(), "3rd Manager  必填"));

                if (string.IsNullOrEmpty(USR_Final_Approver.Text) || string.IsNullOrEmpty(USR_Approver.Text))
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", "请选择3rd Approver之前的经理"));
                }
            }
            else if (USR_Final_Approver_C.Checked && USR_Final_Approver_C.Visible)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, USR_Final_Approver.Text.ToString().Trim(), "2nd Manager  必填"));

                //if (string.IsNullOrEmpty(USR_Approver.Text))
                //{
                //    regs.Add(new Regular(RegularExp.NotEmpty, "", "1st Manager 必填"));
                //}

            }
            //if (USR_Approver_C.Checked && USR_Approver_C.Visible && !string.IsNullOrEmpty(USR_Final_Approver.Text) && !string.IsNullOrEmpty(USR_3rd_Approver.Text) && !string.IsNullOrEmpty(USR_3rd_Approver.Text) && !string.IsNullOrEmpty(USR_4th_Approver.Text) && !string.IsNullOrEmpty(USR_5th_Approver.Text))
            //{
            //    regs.Add(new Regular(RegularExp.NotEmpty,  USR_Approver.Text, "1st Manager 必填"));
            //}
            if (USR_Approver_C.Checked && USR_Approver_C.Visible)
            {
                akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_Approver_L, USR_Approver.Text, "1st Manager 必填");
            }


            if (USR_Inform_Recruiter_C.Checked && USR_Inform_Recruiter_C.Visible)
            {
                akzo_RegularCheck(ref regs, RegularExp.NotEmpty, USR_Inform_Recruiter_L, USR_Inform_Recruiter.Text, "Inform Recruiter 必填");
            }
            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>Users Choose</b></font>");
            }

            return vald_s;
        }
        #endregion

    }
}
